package com.tozed.android.launcher.simpleui;

import com.tozed.android.launcher.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class SimplePageIndicator extends ImageView
{
	private int mCurrentScreen;
	private int mIdcSpaceWidth;
	private int mStartPos;
	private float startPosX;
	private float startPosY;
	private int screenCount;
	private Paint mPaint;
	private Bitmap mInactivePoint;//mSelectPoint;
	private Bitmap mActivePoint;  // mNormalPoint;
	
	private SimpleFolder mSimpleFolder;
	private SimpleWorkspace mWorkspace;
	private SimpleUILauncher mSimple;
	private SimpleWorkspace mFolderWorkspace;
	
	public static abstract interface NaviPointLayoutChange{
		public abstract void layoutNavigationPoint(int paramInt1,int paramInt2, int paramInt3, int paramInt4);
	}
	
	public SimplePageIndicator(Context context){
		super(context);
	}

	public SimplePageIndicator(Context context, AttributeSet attrs){
		super(context, attrs);
		initialize(context);
	}

	public SimplePageIndicator(Context context, AttributeSet attrs, int defStyle){
		super(context, attrs, defStyle);
		initialize(context);
	}
	
	private void initialize(Context paramContext)
	{
	    startPosY = getContext().getResources().getDimension(R.dimen.simple_start_pos_y);
	    // 两个指示图标之前的空间宽度
	    mIdcSpaceWidth = (int)getContext().getResources().getDimension(R.dimen.simple_idc_space_width);
	    mPaint = new Paint();
	    
	    // 选中图标
	    Drawable activeDrawable = paramContext.getResources().getDrawable(R.drawable.ic_indicator_inactive);
	    // 未选中图标
	    Drawable inactiveDrawable = paramContext.getResources().getDrawable(R.drawable.ic_indicator_active);
	    
	    mActivePoint = ((BitmapDrawable)activeDrawable).getBitmap();
	    mInactivePoint = ((BitmapDrawable)inactiveDrawable).getBitmap();
	}
	
	private int position(int startPos, int screen){
		return startPos + screen * mIdcSpaceWidth;
	}

	private int startPosition(){
		int i = (screenCount -1) * mIdcSpaceWidth + mInactivePoint.getWidth();
		return getWidth() - i >> 1;
	}

	public int getCurrentScreen(){
		return mCurrentScreen;
	}
	
	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);

		if (mWorkspace == null && mFolderWorkspace == null)
			return;

		// 计算workspace包含的CellLayout数
		if (mWorkspace == null){
			screenCount = mFolderWorkspace.getChildCount();
		} else{
			screenCount = mWorkspace.getChildCount();
		}
		
		mPaint.setAntiAlias(true);
		mStartPos = startPosition();

		for (int screen = 0; screen < screenCount; screen++)
		{
			startPosX = position(mStartPos, screen);
			if (screen != mCurrentScreen)
				canvas.drawBitmap(mInactivePoint, startPosX, startPosY, mPaint);
			else
				canvas.drawBitmap(mActivePoint, startPosX, startPosY, mPaint);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		super.onTouchEvent(event);
		return false;
	}
	
	// 设置当前屏
	public void setCurrentScreen(int curScreen){
		mCurrentScreen = curScreen;
	}

	public void setSimple(SimpleUILauncher simpleLauncher){
		mSimple = simpleLauncher;
		mWorkspace = mSimple.getWorkspace();
		mCurrentScreen = mWorkspace.getCurrentScreen();
	}

	public void setSimpleFolder(SimpleFolder simpleFolder)
	{
		mSimpleFolder = simpleFolder;
		mFolderWorkspace = mSimpleFolder.getWorkspace();
		mCurrentScreen = mFolderWorkspace.getCurrentScreen();
	}
}
